/***
Clean Balance sheet data
Date: July 2022
Author: IM

This file:
- Clean the balance-sheet data
		i) drop if cafranc==1, 
		ii) Takes care of negative sales
		iii) creates a dummy for manufacturing sectors (mfrg)
- Compute growth rates of various real and nominal variables
- Compute growth rates based on Davis et al's definition
	
Input:
- $datagrowth

Output:
- $datagrowth
- variables: 
		1) same as before
		2) gr_effsalm, gr_caexpor, gr_cafrance, gr_catotal, gr_vaht, gr_vabcf, growth rates of employment, export sales, domestic sales, total sales, before-tax value added, value added
		2) agr_effsalm, agr_caexpor, agr_cafrance, agr_catotal, agr_vaht, agr_vabcf, growth rates computed using Davis et al's definition 
		
	
****************************************************************************/


use $datagrowth, clear
drop if siren==""
cap drop id_f
egen id_f=group(siren)


* Dummy for manufacturing sectors
capture drop mfrg
g mfrg=0
if "$datagrowth"=="$commonpath/brn_wof_$firstyearbrn$lastyearbrn"{
	g _=substr(ape,1,2)
	destring _, replace
	replace mfrg=1 if _>=10 & _<=33
	drop _
}
else if "$datagrowth"=="$datapath/ficus_$firstyearficus$lastyearficusprofil"{
	replace mfrg=1 if naf17=="D"
}

* Harmonize variable names
if "$datagrowth"=="$commonpath/brn_wof_$firstyearbrn$lastyearbrn"{
	g catotal=tot_sales
	g cafranc = dom_sales 
	g caexpor=exp_sales
	g achampr= raw_purchases +delta_inventories_raw 
	g achamar=merch_purchases +delta_inventories_merch
	g effsalm=effec
	g vaht=va
	g saltrai=wages 
}
* A few negative sales which seem to be typos
replace catotal=abs(catotal)
replace cafranc=abs(cafranc)
drop caexpor
g caexpor=catotal-cafranc


* generate growth rates
gcollapse (sum) effec income tot_cost op_cost fin_cost extra_cost participation income_tax tot_revenue op_revenue fin_revenue extra_revenue op_income fin_income before_tax_income extra_income merch_purchases delta_inventories_merch raw_purchases delta_inventories_raw other_ext_cost tax wages social_cont tot_sales tot_sales_merch tot_sales_output tot_sales_services exp_sales exp_sales_merch exp_sales_services dom_sales dom_sales_merch dom_sales_output dom_sales_services delta_inventories_output fin_cost_prov fin_cost_interest fin_cost_ermvts fin_cost_market fin_revenue_inv fin_revenue_otherinv fin_revenue_other fin_revenue_prov_reversal fin_revenue_ermvts fin_revenue_market fixed_assets circulating_assets tot_assets liabilities_equity liabilities_debt liabilities_tot operating_intangible_assets patent_intangible_assets goodwill_intangible_assets other_intangible_assets advance_intangible_assets lands_tangible_assets buildings_tangible_assets equipment_tangible_assets other_tangible_assets receivables_fin_assets other_holdings_fin_assets loans_fin_assets other_fin_assets inventories_raw inventories_goods inventories_serv inventories_inputs inventories_merch net_income convertible_bonds other_bonds bank_loans other_loans debt_suppliers debt_tax debt_bank ctdx other_assets rd_intangible_assets liability_other equity_social_capital empdett equity_other product prodven va vabcf ebe caf owner_equity own_resources stable_debt stable_resources debt total_debt working_capital need_working_capital gross_fixed_assets capital_asset catotal cafranc achampr achamar effsalm vaht saltrai caexpor, by(id_f year siren ape dep datcr datcs ape_rev2 mfrg )

gsort id_f year
xtset id_f year
gen firm_inputs = achamar + achampr
foreach var in effsalm caexpor cafranc catotal vaht vabcf saltrai firm_inputs{
	cap drop lag_`var'
	cap drop gr_`var'
	ge lag_`var'=l.`var'
	ge gr_`var'=`var'/l.`var'-1
	replace gr_`var'=. if `var'==0|l.`var'==0
	label var gr_`var' "Growth rate of `var'"
	label var lag_`var' "`var' in t-1"
	replace `var'=0 if `var'==.
}

* Generate growth rates using the Davis et al definition
foreach var in effsalm caexpor cafranc catotal vaht vabcf saltrai firm_inputs{
	cap drop agr_`var'
	ge agr_`var'=2*(`var'-l.`var')/(`var'+l.`var')
	replace agr_`var'=2 if year!=$firstyearficus & (l.`var'==.|l.`var'==0)
	label var agr_`var' "Growth rate of destination-specific `var', Davis et al def"
}


gsort siren year
compress
save $datagrowth, replace
